// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    inputUserNameValue: '',
    inputPassWordValue: ''
  },
  // 事件处理函数
  bindUserNameInput(e) {
    this.setData({
      inputUserNameValue: e.detail.value
    })
  },
  bindPassWordInput(e) {
    this.setData({
      inputPassWordValue: e.detail.value
    })
  },
  bindLogin() {
    if (this.data.inputUserNameValue.length < 4 || this.data.inputUserNameValue.length > 16) {
      wx.showToast({
        title: '用户名格式错误',
        icon: 'error',
        duration: 1000
      })
      return;
    }
    if (this.data.inputPassWordValue.length < 4 || this.data.inputPassWordValue.length > 16) {
      wx.showToast({
        title: '密码格式错误',
        icon: 'error',
        duration: 1000
      })
      return;
    }
    wx.request({
      url: app.globalData.requestUrl + 'login/index.php',
      method: "post",
      data: {
        "userName": this.data.inputUserNameValue,
        "password": this.data.inputPassWordValue
      },
      header: {
        'content-type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 0) {
          wx.setStorageSync("token", res.data.data)
          wx.switchTab({
            url: "../home/home"
          })
        } else if (res.data.code == 1010) {
          wx.showToast({
            title: '用户名密码错误',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail: (res) => {
        
        wx.showModal({
          title: '提示',
          content: '服务器开小差了，请稍后重试',
          showCancel: false,
          confirmText: "我知道了"
        })
      }
    })
  },
  onLoad() {

  },
  onShow() {
    wx.hideHomeButton({
      success: (res) => {},
    })
  }

})